layui.use('layer', function() {
	var layer = layui.layer;
	var patrn1 = /^[a-zA-Z0-9]{1}([a-zA-Z0-9]|[._,+-@#$%^&*()~`]){2,20}$/;
	var patrn2 = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
    var iphone=/^1[3|4|5|8|9][0-9]\d{4,8}$/;
    var newphone=/^1[345789]\d{9}$/;
	var reg = /^\d{4}$/;
	var regs = /^.{4}$/;
	$(".user").blur(function(){
		var username = $(".user").val();
		if(!patrn1.test(username)) {
			layer.msg("请输以数字.大小写字母和标点符号不允许有空格长度3-20位字符");
			$(".username").css({"border":"1px solid red"})
			return false;
		}else{
			$(".username").css({"border":"1px solid #74767c"})
		}
	})
	
	$(".phone").blur(function(){
		var phone = $(".phone").val();
		 if(!newphone.test(phone)) {
	       	layer.msg("请输入正确手机号");
	       	$(".phone_number").css({"border":"1px solid red"})
	        return false;
   		 }else{
			$(".phone_number").css({"border":"1px solid #74767c"})
		}
	})
	
	// $(".code_num").blur(function(){
	// 	 var code = $(".code_num").val();
	// 	if(!reg.test(code)) {
	// 		$(".code_left").css({"border":"1px solid red"})
	// 		$(".code_right").css({"border":"1px solid red"})
	// 		layer.msg("请输入正确的短信验证码");
	// 		return false
	// 	}else{
	// 		$(".code_left").css({"border":"1px solid #74767c"})
	// 		$(".code_right").css({"border":"1px solid #74767c"})
	// 	}
	// })
	
	$(".pic_code").blur(function(){
		 var verification_numb = $(".pic_code").val();
		if(!regs.test(verification_numb)) {
			$(".verification_left").css({"border":"1px solid red"})
			$(".verification_right").css({"border":"1px solid red"})
			layer.msg("请输入正确的图形验证码");
			return false
		}else{
			$(".verification_left").css({"border":"1px solid #74767c"})
			$(".verification_right").css({"border":"1px solid #74767c"})
		}
	})
	
	//注册
	$('.loginbtn').click(function() {
		var token = $('#token').val();
		var patrn1 = /^[a-zA-Z0-9]{1}([a-zA-Z0-9]|[._,+-@#$%^&*()~`]){2,20}$/;
        var iphone=/^1[3|4|5|8|9][0-9]\d{4,8}$/;
        var newphone=/^1[345789]\d{9}$/;
		var patrn2 = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
			var username = $(".user").val();
			var phone = $(".phone").val();
		if(!patrn1.test(username)) {
			layer.msg("请输以数字.大小写字母和标点符号不允许有空格长度3-20位字符");
			$(".username").css({"border":"1px solid red"})
			return false;
		}else{
			$(".username").css({"border":"1px solid #74767c"})
		}
		 if(!newphone.test(phone)) {
	       	layer.msg("请输入正确手机号");
	       	$(".phone_number").css({"border":"1px solid red"})
	        return false;
   		 }else{
			$(".phone_number").css({"border":"1px solid #74767c"})
		}
		var reg = /^\d{4}$/;
		var code = $(".code_num").val();
		if(!reg.test(code)) {
			$(".code_left").css({"border":"1px solid red"})
			$(".code_right").css({"border":"1px solid red"})
			layer.msg("请输入正确的短信验证码");
			return false
		}else{
			$(".code_left").css({"border":"1px solid #74767c"})
			$(".code_right").css({"border":"1px solid #74767c"})
		}
		var pass = $(".pass").val();
		if(!patrn1.test(pass)) {
			layer.msg("请输入以数字.大小写字母和标点符号不允许有空格长度3-20位字符");
			$(".password").css({"border":"1px solid red"})
			return false
		}else{
			$(".password").css({"border":"1px solid #74767c"})
		}
		var regs = /^.{4}$/;
		var verification_numb = $(".pic_code").val();
		 if(!regs.test(verification_numb)) {
		 	$(".verification_left").css({"border":"1px solid red"})
			$(".verification_right").css({"border":"1px solid red"})
       		layer.msg("请输入正确的图形验证码");
        		return false
    		}else{
    			$(".verification_left").css({"border":"1px solid #74767c"})
			$(".verification_right").css({"border":"1px solid #74767c"})
    		}
		


		var data = {
			'username': username,
			'phone':phone,
			'password': pass,
			'code': code,
			'_token': token,
			'validateCode': verification_numb
		}

		url = $('#url').val();
		var index = layer.load(2, {
			shade: [0.2, '#1d89fb'] //0.1透明度的白色背景
				,
			time: 500,
			end: function(e) {
				$.ajax({
					url: '/doregister',
					type: "POST",
					data: data,
					beforeSend: function() {
                        $(".loginbtn").attr("disabled",true);
					},
					success: function(ret) {
						if(ret.code == 0) {
                            window.location.href='/login';
						} else {
                            layui.layer.msg(ret.msg, {
                                icon: 6,
                                time: 2000
                            });
						}
					}
				});
			}
		});

	});
	//计时器
	var wait = 60;
	var isclick = true;

	function time(o) {
		wait--;
		o.html(wait);
		if(wait == 0) {
			isclick = true;
			o.html('重新发送');
			wait = 60;
			return false;
		}
		isclick = false;
		setTimeout(function() {
				time(o);
			},
			1000)
	}
	$('#getCode').click(function() {
		var _this=$(this);
		var token = $('#token').val();
		var user=$(".user").val();
		var pass=$(".pass").val();
		var phone=$(".phone").val();
		var pic_code=$(".pic_code").val();
        if(user !='' &&phone!='' && pass !='' &&pic_code!=''){
        }else{
            layer.msg("内容不能为空");
            return false;
        }
		if(isclick) {
			var phone = $(".phone").val();
            var iphone=/^1[3|4|5|8|9][0-9]\d{4,8}$/;
            var newphone=/^1[345789]\d{9}$/;
            var patrn2 = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
			if(!newphone.test(phone)) {
				layer.msg("请输入正确的手机号");
				return false;
			}

			var data = {
				'validateCode':pic_code,
				'mobile': phone,
				'action': 'register',
				'_token': token
			};

			$.ajax({
				url: '/code',
				type: "post",
				data: data,
                success: function(ret){
                    if(ret.code != 0)
                    {
                        layui.layer.msg(ret.msg);
                    }else{
                        time(_this);
                    }
                }
            });


		}
	})

	//图片验证更换
	$('.pic').click(function() {
		$.post("", {}, function(data) {
			if(data.code == 0) {
				$('.pic').attr('src', data.src);
			}
		});
	})
});